﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Threading;
using RobotCtrl;
using RobotView;

namespace Test_WinCE_LED_Switch
{
    public partial class Form1 : Form
    {
        private RobotConsole rconsole;
        private Thread lauflichtThread;
        private Drive driveconsole;

        public Form1()
        {
            InitializeComponent();
            rconsole = new RobotConsole(RunMode.REAL);
            driveconsole = new Drive(RunMode.REAL);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            /* 1st Test
            r.Lamps[1] = true;
            bool a = r.Switches[1];
            */
            lauflichtThread = new Thread(lauflichtRun);
            lauflichtThread.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            driveconsole.Power = true;
            //driveconsole.LeftMotor.Speed = 1;
            //driveconsole.LeftMotor.Go();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            driveconsole.Close();
            //driveconsole.LeftMotor.Stop();
        }

        private void Form1_Closing(object sender, CancelEventArgs e)
        {
            //Wenn extra-Threads da sind müssen diese hier explizit mit Thread.Abort()
            // gekillt werden. Da WinCE diese nicht automatisch beendet.
            if (lauflichtThread != null)
            {
                lauflichtThread.Abort();
            }
        }

        private void lauflichtRun()
        {
            while (rconsole.Switches[3])
            {
                rconsole.Lamps.Data = 1;
                while (rconsole.Switches[0])
                {
                    Thread.Sleep(100);
                    if (rconsole.Lamps.Data == 0x8)
                    {
                        rconsole.Lamps.Data = 1;
                        continue;
                    }
                    rconsole.Lamps.Data = rconsole.Lamps.Data << 1;
                }
            }
        }

    }
}